//
//  TabBarController.m
//  News
//
//  Created by 徐涵菲 on 2018/4/22.
//  Copyright © 2018年 Wangly. All rights reserved.
//

#import "TabBarController.h"
#import "TabBar.h"
#import "TabBar2.h"
#import "HomeViewController.h"
#import "MessageViewController.h"
#import "DiscoverViewController.h"
#import "ProfileViewController.h"
#import "NavigationController.h"


@interface TabBarController ()<TabBarDelegate>
/**
 *  自定义TabBar
 */

@property (nonatomic, weak) TabBar2 *customTabBar;

@end
@implementation TabBarController

-(instancetype)init
{
    if (self=[super init]) {
        DDLogDebug(@"%d %s",__LINE__,__func__);
    }
    return self;
}


// init方法内部会调用initWithNibName方法, 所以在自定义控制器时可以把初始化的代码放到initWithNibName方法中, 以后无论是通过init方法还是通过initWithNibName都可以初始化
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        DDLogDebug(@"%d %s",__LINE__,__func__);
        // 4.创建子控制器
        
        UIViewController *home = [self addControllerWithClass:[HomeViewController class] title:@"首页" image:@"tabbar_home" selectedImage:@"tabbar_home_selected"];
        UIViewController *message = [self addControllerWithClass:[MessageViewController class] title:@"消息" image:@"tabbar_message_center" selectedImage:@"tabbar_message_center_selected"];
        
        
        //        UIViewController *other = [self addControllerWithClass:[UIViewController class] title:@"" image:@"" selectedImage:@""];
        
        UIViewController *discover = [self addControllerWithClass:[DiscoverViewController class] title:@"发现" image:@"tabbar_discover" selectedImage:@"tabbar_discover_selected"];
        UIViewController *profile = [self addControllerWithClass:[ProfileViewController class] title:@"我" image:@"tabbar_profile" selectedImage:@"tabbar_profile_selected"];
        //        self.viewControllers = @[home, message,discover, profile];
    }
    return self;
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    /*// 1.创建自定义TabBar
     TabBar *tabBar = [[TabBar alloc] init];
     DDLogDebug(@"替换前 %@", self.tabBar);
     // 2.用自定义的CZTabBar替换系统的TabBar
     [self setValue:tabBar forKeyPath:@"tabBar"];
     DDLogDebug(@"替换后 %@", self.tabBar);
     */
    // 1.创建自定义CZTabBar2
    TabBar2 *tabBar = [[TabBar2 alloc] init];
    
    // 设置代理
    tabBar.delegate = self;
    
    // 2.设置CZTabBar2的frame
    tabBar.frame = self.tabBar.frame;
    // 3.添加CZTabBar2到父控件
    [self.view addSubview:tabBar];
    self.customTabBar=tabBar;
    // self.customTabBar = tabBar;
    // 4.移除系统自带的tabbar
    [self.tabBar removeFromSuperview];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //    DDLogDebug(@"subviews= %@",self.tabBar.subviews);
    for (UIView *child in self.tabBar.subviews) {
        child.frame=CGRectMake(0, 0, 100, 100);
    }
}

/**
 *  根据一个类创建一个控制器
 *
 *  @param class         控制器对应的类
 *  @param title         标题
 *  @param image         默认状态的图片
 *  @param selectedImage 选择状态图片
 *
 *  @return 创建好的控制器
 */
- (UIViewController *)addControllerWithClass:(Class)class title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    /*
     注意: 不是所有的控制器都是通过init方法初始化的
     */
    UIViewController *vc = [[class alloc] init];
    return [self addControllerWithVc:vc title:title image:image selectedImage:selectedImage];
}

/**
 *  根据一个创建好的控制器初始化控制器
 *
 *  @param vc            控制器
 *  @param title         标题
 *  @param image         默认图片
 *  @param selectedImage 选中图片
 *
 *  @return 初始化之后的控制器
 */
- (UIViewController *)addControllerWithVc:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    //同时设置选项卡和导航条的标题
    vc.title=title;
    
    vc.tabBarItem.image = [UIImage imageNamed:image];
    /*
     iOS7需要通过代码告诉系统不要用tabBar.tintColor来渲染选中图片
     */
    // 1.创建图片
    UIImage *newImage =  [UIImage imageNamed:selectedImage];
    // 2.告诉系统原样显示
    newImage = [newImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 3.设置图片
    vc.tabBarItem.selectedImage = newImage;
    
    // 设置tabBarButton的标题颜色
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor orangeColor]} forState:UIControlStateSelected];
    
    // 包装一个导航控制器
    NavigationController *nav = [[NavigationController alloc] initWithRootViewController:vc];
    
    // 添加控制器到tabbarcontroller
    [self addChildViewController:nav];
    
    // 每添加一个子控制器就要创建一个对应的按钮
    [self.customTabBar addItem: vc.tabBarItem];
    
    return vc;

}

#pragma mark - TabBarDelegate
- (void)tabBar:(TabBar2 *)tabBar selectedBtnFrom:(NSInteger)from to:(NSInteger)to
{
    DDLogDebug(@"from = %tu  to = %tu", from, to);
    // 切换控制器
    // 1.取出当前选中按钮对应的控制器
    //        UIViewController *vc = self.childViewControllers[to];
    //        self.selectedViewController = vc;
    
    self.selectedIndex=to;
    
}


@end
